<?php
namespace app\index\controller;
use QL\QueryList;
use phpmailer\PHPMailer;

use think\Db;

class Index
{
    public function index()
    {
        return 'hello world';
    }

    public function test()
    {
        //爬取的内容规则
        //爬取试用页面
        $params = [
            'title' => [".e-title", "text"],
            'imgUrl' => ['.img-box img', "src"],
            'eTag' => ['div.e-tag-list', "html"],
            'peopleNum' => ['font.red', "text"],
            'peopleNumfr' => ['.e-query', "text"],
            'otherTag' => ['.e-item-hide', "html"]
        ];
        for ($j = 0; $j < 1; $j++) {
            $ql = QueryList::Query('http://www.jiguo.com/event/index/page/' . $j . '/act/index.html', $params,"","UTF-8","UTF-8","true")->getData(function ($item) {
                $item['otherTag'] = QueryList::Query($item['otherTag'], [
                    'otherTag' => ['span', 'text']
                ],'',"UTF-8","UTF-8","true")->data;
                $item['eTag'] = QueryList::Query($item['eTag'], [
                    'eTag' => ['span', 'text']
                ],"","UTF-8","UTF-8","true")->data;

                //以下将专享试用标签数组扁平化处理
                $return = [];
                array_walk_recursive($item['eTag'], function ($x, $index) use (&$return) {
                    $return[] = $x;
                    return $return;
                });
                $item['eTag'] = $return;
                foreach ($item['eTag'] as $k => $v) {
//                print_r($k);
                    $item['eTag' . $k] = $v;
                }
                unset($item['eTag']);

                //以下将hover标签数组扁平化处理
                $return2 = [];
                array_walk_recursive($item['otherTag'], function ($s, $index) use (&$return2) {
                    $return2[] = $s;
                    return $return2;
                });
                $item['otherTag'] = $return2;
                foreach ($item['otherTag'] as $k => $v) {
                    $item['otherTag' . $k] = $v;
                }
                unset($item['otherTag']);

                return $item;
            });

           print_r($ql);
            for ($i = 0; $i < count($ql); $i++) {
//                $content = mb_convert_encoding( $ql[$i], 'UTF-8', 'auto' );
//               Db::table("trial")->insert($ql[$i]);
            }
        }

//        爬取新品页面
        $params2 = [
            'title'=>[".d-title","text"],
            'imgUrl'=>['.choice-img-box img',"src"],
            'dNum'=>['.d-number',"html"]
        ];

        for($j=0;$j<50;$j++){
            $ql2 = QueryList::Query('http://www.jiguo.com/product/index/type/2/page/'.$j.'.html',$params2)->getData(function($item) {
                $item['dNum'] = QueryList::Query($item['dNum'], [
                    'dNum' => ['span', 'text']
                ])->data;

                //以下将标签数组扁平化处理
                $return = [];
                array_walk_recursive($item['dNum'], function ($x, $index) use (&$return) {
                    $return[] = $x;
                    return $return;
                });
                $item['dNum'] = $return;
                foreach ($item['dNum'] as $k => $v) {
//                print_r($k);
                    $item['dNum' . $k] = $v;
                }
                unset($item['dNum']);
                return $item;
           });
//           print_r( $ql2);
            for($i=0;$i<count($ql2);$i++){
//                Db::table("product")->insert($ql2[$i]);
            }
        }
        //使用QueryList 类中静态方法(不需要实例化可以直接使用的方法)
//            爬取轮播图
        $params3 = [
            'imgUrl'=>['.banner-img-bg img',"src"],
            'url'=>['.banner-img-bg a',"href"],
        ];
            $ql3 = QueryList::Query('http://www.jiguo.com/index/index.html',$params3)->data;
//            print_r( $ql3);
            for($i=0;$i<count($ql3);$i++){
//                Db::table("banner")->insert($ql3[$i]);
            }

    }


    static function mail($data=["uid"=>"39"]){
        $mail = new PHPMailer();
        //配置发送邮件的操作
//        print_r($mail);
        $mail->isSMTP();//开启smtp服务
        $mail->CharSet = "UTF-8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
        $mail->Host = "smtp.qq.com";// 发送方的SMTP服务器地址
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = '234543698@qq.com';//// 发送方的
        $mail->Password = 'zryeqhjgdhuhbhdi';//客户端授权密码,而不是邮箱的登录密码！
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式
        $mail->Port = 465;//  qq端口465或587）
        $mail->From = '234543698@qq.com';//设置在收件人中显示发件的地址
        $mail->FromName = '极果账户激活邮件通知服务';//设置邮件发送者的名字
        $mail->Subject ="极果激活通知邮件";// 邮件标题
        $mail->IsHTML(true);//内容是否为html 内容
        $mail->AddAddress($data["email"]); //添加收件人
        $mail->AddCC('234543698@qq.com');//添加抄送收件人
        $mail->SMTPDebug = true;//开启debug 调试
        $mail->Body = "<p><a target='_balnk' href='//jq.com/active?name="+$data['uid']+"'>请点击链接激活</a></p><br>";//邮件内容
        $mail->isHTML(true);
        $result =$mail->send();//发送邮件
//        print_r($data);
//        return $result;

    }

    static function active(){
        $name=input("userName");
//        print_r($name);
        Db::table("users")->where("uid",39)->update(["status"=>"1"]);
    }
}
